import type { HistoryReportType } from '@/types/histroyReport'
// 战报分析
export default class HistoryReport {
  constructor() {

  }

  static getUserReport(arr): HistoryReportType {
    if (arr.length === 0) return
    // KDA
    let kills = 0
    let deaths = 0
    let assists = 0

    let losenum = 0 // 失败次数
    let winNum = 0 // 胜利次数
    const heros = [] // 英雄使用情况
    for (let i = 0; i < arr.length; i++) {
      const participant = arr[i]?.participants?.[0]
      if (!participant) continue
      const { stats } = participant

      // 英雄使用情况
      const hero = { championId: participant.championId, num: 1 }

      const heroIndex = heros.findIndex(item => item.championId === participant.championId)
      if (heroIndex === -1) {
        heros.push(hero)
      } else {
        heros[heroIndex].num++
      }

      // KDA
      kills += stats.kills
      deaths += stats.deaths
      assists += stats.assists

      // 胜利次数
      if (stats.win) {
        winNum++
      } else {
        losenum++
      }
    }
    const winRate = Number(((winNum / (winNum + losenum)) * 100).toFixed(1))
    const KDA = ((kills + assists) / deaths).toFixed(2)
    const herosSort = heros.sort((a, b) => b.num - a.num)
    return {
      KDA,
      winRate,
      herosSort,
      losenum,
      winNum,
    }
  }
}
